package com.system.connect.springboot_flowers.controller;

import com.system.connect.springboot_flowers.dto.AlipayCreateDTO;
import com.system.connect.springboot_flowers.dto.AlipayNotifyDTO;
import com.system.connect.springboot_flowers.service.AlipayService;
import com.system.connect.springboot_flowers.utils.R;
import com.system.connect.springboot_flowers.vo.AlipayQrCodeVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/alipay")
public class AlipayController {

    @Autowired
    private AlipayService alipayService;

    @PostMapping("/createQrCode")
    public R<AlipayQrCodeVO> createQrCode(@RequestBody AlipayCreateDTO dto) {
        log.info("创建支付宝支付二维码，订单号：{}，金额：{}，商品名称：{}", 
                dto.getOrderId(), dto.getAmount(), dto.getSubject());
        AlipayQrCodeVO result = alipayService.createQrCode(dto);
        return result.getSuccess() ? R.success(result) : R.error("创建支付二维码失败");
    }

    @PostMapping("/notify")
    public String notify(@RequestParam Map<String, String> params) {
        log.info("支付宝支付回调通知：{}", params);
        alipayService.handleNotify(params);
        return "success";
    }

    @GetMapping("/checkStatus")
    public R<Boolean> checkPaymentStatus(@RequestBody AlipayNotifyDTO dto) {
        log.info("查询支付状态，订单号：{}", dto.getOrderNo());
        Boolean isPaid = alipayService.checkPaymentStatus(dto.getOrderNo());
        return R.success(isPaid);
    }
} 